/*
   Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
   Original sources are available at www.latestbit.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package org.latestbit.smartobjects.encrypters;

import java.nio.ByteBuffer;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.EncodedKeySpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;

import org.latestbit.smartobjects.io.ResizableByteBuffer;

public class StdAsymmetricEncrypter extends StdAbstractDataEncrypter implements IAsymmetricDataEncrypter {		
	
	public static final String DEFAULT_ALGORITHM_OPTIONS = "/ECB/PKCS1Padding";
	public static final String DEFAULT_ALGORITHM = "RSA" + DEFAULT_ALGORITHM_OPTIONS;
	public static final int KEY_LENGTH = 1024;
	
	private PublicKey publicKey;
	private PrivateKey privateKey;
	
	public StdAsymmetricEncrypter() {
		super(DEFAULT_ALGORITHM);
	}
	
	public StdAsymmetricEncrypter(String algorithm) {
		super(algorithm);
	}	
	
	@Override
	public void initCipher(PrivateKey key) throws InvalidKeyException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException {
		// this.algorithm = key.getAlgorithm();
		this.privateKey = key;
		decCipher = Cipher.getInstance(algorithm);
		decCipher.init(Cipher.DECRYPT_MODE, key);
	}

	@Override
	public void initCipher(PublicKey key) throws InvalidKeyException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException {
		// this.algorithm = key.getAlgorithm();
		this.publicKey = key;
		encCipher = Cipher.getInstance(algorithm);
		encCipher.init(Cipher.ENCRYPT_MODE, key);
	}
	
	@Override
	public PublicKey decodePublicKey(ByteBuffer buffer) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidKeySpecException {
		byte[] encodedKey = new byte[buffer.limit() - buffer.position()];
		buffer.get(encodedKey);
		EncodedKeySpec keySpec = new X509EncodedKeySpec ( encodedKey );
		return KeyFactory.getInstance(algorithmWithoutOptions).generatePublic(keySpec);
	}

	@Override
	public void encodePublicKey(PublicKey key, ResizableByteBuffer buffer ) {		
		buffer.put(key.getEncoded());
	}

	@Override
	public PrivateKey getPrivateKey() {
		return this.privateKey;
	}

	@Override
	public PublicKey getPublicKey() {
		return this.publicKey;
	}

	@Override
	public KeyPair generateKeyPair() throws NoSuchAlgorithmException {
		KeyPairGenerator keyGen;
		keyGen = KeyPairGenerator.getInstance(this.algorithmWithoutOptions);
		keyGen.initialize(KEY_LENGTH);
        return keyGen.generateKeyPair();
	}

	
}
